package com.rmfyzxfw.caseprocess.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rmfyzxfw.caseprocess.boot.result.page.Pagination;
import com.rmfyzxfw.caseprocess.enums.EnableStatusEnum;
import com.rmfyzxfw.caseprocess.model.cmd.*;
import com.rmfyzxfw.caseprocess.model.converter.DataNodeConverter;
import com.rmfyzxfw.caseprocess.model.param.DataNodeConfigListParam;
import com.rmfyzxfw.caseprocess.model.param.DataNodeListParam;
import com.rmfyzxfw.caseprocess.model.po.DataNode;
import com.rmfyzxfw.caseprocess.model.po.Process;
import com.rmfyzxfw.caseprocess.model.vo.DataNodeDetailVO;
import com.rmfyzxfw.caseprocess.model.vo.DataNodeListVO;
import com.rmfyzxfw.caseprocess.model.vo.ProcessDataNodeListVO;
import com.rmfyzxfw.caseprocess.service.DataNodeService;
import com.rmfyzxfw.caseprocess.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("api/v3/dataNode")
@RequiredArgsConstructor
@Api(tags = "数据节点")
public class DataNodeController {
    private final DataNodeService dataNodeService;
    private final ProcessService processService;
    private final DataNodeConverter dataNodeConverter;

    @PostMapping("add")
    @ApiOperation(value = "添加数据节点")
    public void addDataNode(@RequestBody @Valid DataNodeCmd cmd) {
        DataNode dataNode = dataNodeConverter.cmd2do(cmd);
        dataNode.setDataNodeId(IdUtil.simpleUUID());
        dataNode.setDeadline(0);
        dataNode.setStatus(EnableStatusEnum.DISABLE.getCode());
        dataNodeService.save(dataNode);
    }

    @PostMapping("editDataNode")
    @ApiOperation(value = "编辑数据节点")
    public void editDataNode(@RequestBody @Valid DataNodeEditCmd cmd) {
        DataNode dataNode = dataNodeConverter.cmd2do(cmd);
        dataNodeService.update(dataNode, Wrappers.<DataNode>lambdaUpdate().eq(DataNode::getDataNodeId, dataNode.getDataNodeId()).last("limit 1"));
    }

    @PostMapping("manageDataNode")
    @ApiOperation(value = "禁用/启用数据节点")
    public void manageDataNode(@RequestBody @Valid DataNodeManageCmd cmd) {
        DataNode dataNode = dataNodeConverter.cmd2do(cmd);
        dataNodeService.update(dataNode, Wrappers.<DataNode>lambdaUpdate().eq(DataNode::getDataNodeId, dataNode.getDataNodeId()).last("limit 1"));
    }

    @PostMapping("addDataNodeItem")
    @ApiOperation(value = "节点添加数据项")
    public void addDataNodeItem(@RequestBody @Valid DataNodeItemCmd cmd) {
        dataNodeService.addDataNodeItem(cmd);
    }

    @PostMapping("removeDataNodeItem")
    @ApiOperation(value = "删除节点数据项")
    public void removeDataNodeItem(@RequestBody @Valid DataNodeItemDelCmd cmd) {
        dataNodeService.removeDataNodeItem(cmd);
    }

    @GetMapping("list")
    @ApiOperation(value = "数据节点列表")
    public Pagination<DataNodeListVO> list(DataNodeListParam param) {
        return dataNodeService.list(param);
    }

    @GetMapping("configList")
    @ApiOperation(value = "数据节点列表-流程配置使用")
    public Pagination<DataNodeListVO> configList(DataNodeConfigListParam param) {
        return dataNodeService.configList(param);
    }

    @GetMapping("list/{processId}")
    @ApiOperation(value = "获取流程中配置的数据节点 不包含子流程")
    public List<ProcessDataNodeListVO> processDataNodeList(@PathVariable String processId) {
        return dataNodeService.processDataNodeList(processId);
    }

    @GetMapping("{dataNodeId}")
    @ApiOperation(value = "数据节点详情")
    public DataNodeDetailVO detail(@PathVariable String dataNodeId) {
        DataNode dataNode = dataNodeService.getOne(Wrappers.<DataNode>lambdaQuery().eq(DataNode::getDataNodeId, dataNodeId));
        DataNodeDetailVO dataNodeDetailVO = dataNodeConverter.do2vo(dataNode);
        if(StringUtils.isNotBlank(dataNode.getChildrenProcessId())){
            Process process = processService.getOne(Wrappers.<Process>lambdaQuery().eq(Process::getProcessId, dataNode.getChildrenProcessId()));
            dataNodeDetailVO.setChildrenProcessName(process.getProcessName());
        }

        return dataNodeDetailVO;
    }
}
